// pages/functions/jumpPage/popularize/adjustProduct.js

// import {
//   list,adminlist,save,update,del
// } from '../../../../api/product'
import {
  list
} from '../../../../api/product'
import {
  pricelist,updatePrice,
} from '../../../../api/orderAdmin'
const util = require('../../../../utils/util')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberId:'',
    brandList: [],
    isProduct: false,
    isScreen: false,
    brandId: '',
    goods: '',
    searchList: [],
    historyList: [],
    focus: false,
    pageNum: 1,
    pageSize:10,
    total:0,
    status: false,
    showType: 'transverse',
    isNullShow: false,
    isTitleShow: '',
    isShow: false,
    adjustProduct:[],
    statusBarHeight:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.memberId = options.id
    this.searchGoods()
    this.setNavSize()
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight,
    });
  },

  searchGoods(data) {
    var hash = {};
    // console.log(data,"data");
    if(data != undefined){
      this.data.goods = data.detail.value.replace(/\s+/g, '')
    }else{
      this.data.goods = ""
    }
    this.data.searchList = []
    // this.data.brandList = []
    // console.log(this.data.goods,"goods");
    if (this.data.goods != "") {
      list({
        name: this.data.goods,
        mchId: app.globalData.mchId
      }).then(res => {
        // console.info(res)
        this.data.searchList = res.data.data
        if (this.data.searchList.length > 0) {
          this.data.isTitleShow = false
        }
        this.setData({
          brandList: this.data.brandList,
          isTitleShow: this.data.isTitleShow,
          goods: this.data.goods,
          searchList: this.data.searchList
        })
      })
    } else if (this.data.goods.length == 0) {
      this.data.searchList = []
      this.setData({
        searchList: this.data.searchList,
        goods: this.data.goods
      })
    }
    if (this.data.goods.length != 0) {
      this.data.isShow = true
    } else {
      this.data.isShow = false
    }
    this.setData({
      isShow: this.data.isShow
    })
  },

  bindconfirm(e) {
    if (this.data.searchList.length > 0) {
      this.data.isProduct = true
    } else {
      this.data.isProduct = false
    }
    if (this.data.goods.length != 0 && this.data.searchList.length == 0) {
      this.data.isNullShow = true
      this.data.isTitleShow = false
    } else if (this.data.goods.length != 0 && this.data.searchList.length != 0) {
      this.data.isTitleShow = true
      this.data.isNullShow = false
    }
    this.setData({
      isProduct: this.data.isProduct,
      isNullShow: this.data.isNullShow,
      isTitleShow: this.data.isTitleShow
    })
  },

  isFocus() {
    if (this.data.searchList.length > 0 && this.data.isTitleShow == true) {
      this.data.isTitleShow = false
    } else if (this.data.isNullShow == true) {
      this.data.isNullShow = false
    }
    this.data.focus = true
    this.setData({
      isNullShow: this.data.isNullShow,
      isTitleShow: this.data.isTitleShow,
      focus: this.data.focus
    })
  },
  empty() {
    this.data.goods = ''
    this.data.isShow = false
    this.data.searchList = []
    this.setData({
      searchList: this.data.searchList,
      isShow: this.data.isShow,
      goods: this.data.goods,
    })
  },

  toItem(e){
    // console.log('e',e.target.dataset)
    let _this = this;
    updatePrice({
        memberId:this.data.memberId,
        productId:e.target.dataset.id,
        price:e.target.dataset.price,
      }).then(res=>{
        // console.log("res",res.data)
        if(res.type == "success"){
          // this.loadAllProduct()
          _this.data.adjustProduct.push(res.data)
          if(_this.data.adjustProduct.length>0){
            this.data.isProduct=true
          }else{
            this.data.isProduct=false
          }
          this.data.isTitleShow=true
          this.setData({
            brandList:this.data.brandList,
            historyList: this.data.historyList,
            isProduct:this.data.isProduct,
            isScreen:this.data.isScreen,
            adjustProduct:this.data.adjustProduct,
            isTitleShow:this.data.isTitleShow
          })
        }
        // console.log("adjustProduct",this.data.adjustProduct)
      })
  },
  loadAllProduct(){
    pricelist({
      pageNum:this.data.pageNum,
      pageSize:this.data.pageSize,
      memberId:this.data.memberId,
    }).then(res=>{
      this.data.adjustProduct = res.data.data
      this.data.total = res.data.total
      if(this.data.adjustProduct.length>0){
        this.data.isProduct=true
      }else{
        this.data.isProduct=false
      }
      this.data.isTitleShow=true
      this.setData({
        brandList:this.data.brandList,
        isProduct:this.data.isProduct,
        isScreen:this.data.isScreen,
        adjustProduct:this.data.adjustProduct,
        isTitleShow:this.data.isTitleShow
      })
    })
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
      // console.log(sysinfo)
    if (!isiOS) {
      navHeight = 46;
    } else {
      navHeight = 42;
    }
    that.setData({
      statuss: statusHeight,
      navHeight: navHeight
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadAllProduct()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      this.data.pageNum=1
      pricelist({
        pageNum:this.data.pageNum,
        name:this.data.goods,
        mchId: app.globalData.mchId,
      }).then(res=>{  
        this.data.total = res.data.total
        this.data.adjustProduct=res.data.data
        this.setData({
          adjustProduct:this.data.adjustProduct
        })
      })
      wx.stopPullDownRefresh()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pageNum * this.data.pageSize >= this.data.total){
      return;
    }
    this.data.pageNum+=1
    if(this.data.pageNum==1){
      this.data.searchList=[]
    }
    pricelist({
      memberId:this.memberId,
      mchId: app.globalData.mchId,
      pageNum:this.data.pageNum,
      pageSize:this.data.pageSize,
    }).then(res=>{  
      this.data.total = res.data.total
      res.data.data.forEach(item=>{
        this.data.adjustProduct.push(item)
      })
      this.setData({
        adjustProduct:this.data.adjustProduct
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})